package com.iwhalecloud.citybrain.flow.platform.client.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author lilh
 * @date 2019-02-18 16:00
 */
public class HttpClientUtils {

    private static final Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);

    private static final String ENCODE = "UTF-8";

    public HttpClientUtils() {
    }

    public static String sendHttpPost(String url, String json) throws RuntimeException {
        return sendHttpPost(url, json, "UTF-8");
    }

    public static String sendHttpPost(String url, String json, String encode) {
        encode = encode == null ? "UTF-8" : encode;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = null;
        String result = null;
        httpPost = new HttpPost(url);
        httpPost.setHeader("token", "test_token");

        try {
            InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(json.getBytes(encode)), ContentType.APPLICATION_JSON);
            httpPost.setEntity(inputStreamEntity);
            HttpResponse response = httpclient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity(), encode);
            }

            return result;
        } catch (ClientProtocolException var8) {
            logger.error("ClientProtocolException异常 : " + var8);
            throw new RuntimeException(var8);
        } catch (IOException var9) {
            logger.error("IOException异常 :  " + var9);
            throw new RuntimeException(var9);
        } catch (Exception var10) {
            logger.error("异常 :  " + var10);
            throw new RuntimeException(var10);
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static String sendHttpPost(String url, Map<String, String> paramMap) {
        return sendHttpPost(url, paramMap, "UTF-8");
    }

    public static String sendHttpPost(String url, Map<String, String> paramMap, String encode) {
        encode = encode == null ? "UTF-8" : encode;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = null;
        String result = null;
        httpPost = new HttpPost(url);
        httpPost.setHeader("token", "test_token");

        try {
            List<NameValuePair> nvps = new ArrayList<>();
            Iterator iterator = paramMap.entrySet().iterator();

            while(iterator.hasNext()) {
                Map.Entry<String, String> paramEntry = (Map.Entry)iterator.next();
                NameValuePair nameValuePair = new BasicNameValuePair((String)paramEntry.getKey(), (String)paramEntry.getValue());
                nvps.add(nameValuePair);
            }

            httpPost.setEntity(new UrlEncodedFormEntity(nvps));
            HttpResponse response = httpclient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity(), encode);
            }
        } catch (ClientProtocolException var10) {
            logger.error("ClientProtocolException异常 : " + var10);
        } catch (IOException var11) {
            logger.error("IOException异常 :  " + var11);
            throw new RuntimeException(var11);
        } catch (Exception var12) {
            logger.error("异常 :  " + var12);
            throw new RuntimeException(var12);
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    public static String sendHttpPost(String url, HttpEntity httpEntity, String encode) throws Exception {
        encode = encode == null ? "UTF-8" : encode;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = null;
        String result = null;
        httpPost = new HttpPost(url);
        httpPost.setHeader("token", "test_token");

        try {
            httpPost.setEntity(httpEntity);
            HttpResponse response = httpclient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity(), encode);
            }

            return result;
        } catch (ClientProtocolException var7) {
            logger.error("ClientProtocolException异常 : " + var7);
            return result;
        } catch (IOException var8) {
            logger.error("IOException异常 :  " + var8);
            throw var8;
        } catch (Exception var9) {
            logger.error("异常 :  " + var9);
            throw var9;
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static String sendHttpPost(String url, HttpEntity httpEntity, String encode, String token) throws Exception {
        encode = encode == null ? "UTF-8" : encode;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = null;
        String result = null;
        httpPost = new HttpPost(url);
        httpPost.setHeader("token", token);

        try {
            httpPost.setEntity(httpEntity);
            HttpResponse response = httpclient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity(), encode);
            }

            return result;
        } catch (ClientProtocolException e) {
            logger.error("ClientProtocolException异常 : " + e);
            return result;
        } catch (IOException e) {
            logger.error("IOException异常 :  " + e);
            throw e;
        } catch (Exception e) {
            logger.error("异常 :  " + e);
            throw e;
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static boolean testHttpPost(String url) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = null;
        boolean result = false;
        httpPost = new HttpPost(url);
        httpPost.setHeader("token", "test_token");

        try {
            HttpResponse response = httpclient.execute(httpPost);
            logger.info("-----http连接状态：" + response.getStatusLine().getStatusCode());
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = true;
            }
        } catch (ClientProtocolException var5) {
            logger.error("ClientProtocolException异常 : " + var5);
        } catch (IOException var6) {
            logger.error("IOException异常 :  " + var6);
        } catch (Exception var7) {
            logger.error("异常 :  " + var7);
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return result;
    }
}
